<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件

define("contactTable", "user_contact"); // 客户联系user_contact表，不加前缀
define("wxPathTable", "wx_path"); // 小程序的页面路径wx_path表，不加前缀
define("bannerTable", "banner"); // banner表，不加前缀
define("userCouponTable", "user_coupon"); // 用户优惠券user_coupon表，不加前缀
define("lineMenuTable", "line_menu"); // 线路菜单line_menu表，不加前缀
define("formIdTable", "form_id"); // form_id表，不加前缀
define("transferTable", "transfer"); // transfer提现表，不加前缀
define("orderCommissionTable", "order_commission"); // order_commission订单分佣表，不加前缀
define("refundTable", "refund"); // refund退款表，不加前缀
define("cronJobTable", "cron_job"); // cron_job定时任务表，不加前缀
define("verifierTable", "verifier"); // 核销员任务表，不加前缀
define("personalInvoiceTable", "personal_invoice"); // 个人发票表，不加前缀
define("smsRecordTable", "sms_record"); // 短信记录表，不加前缀
define("wxAppUpgradeTable", "wxapp_upgrade"); // 小程序更新记录表，不加前缀

//生成密码
function generate_password($password, $salt){
    if(empty($password) || empty($salt)){
        return false;
    }
    return sha1("$password-$salt-" . config("authKey"));
}

//随机生成密码加密串
function local_salt($length = 8) {
    $result = '';
    while(strlen($result) < $length) {
        $result .= sha1(uniqid('', true));
    }
    return substr($result, 0, $length);
}

//把p_id转为pId
function parse_name($name)
{
    return preg_replace_callback('/_([a-zA-Z])/', function ($match) {
        return strtoupper($match[1]);
    }, $name);
}

//转换静态资源
function tome_dia($file, $url = true){
    if(empty($file))
        return '';
    if(strpos($file, "http") === 0) {
        return $file;
    }
    if($url) {
        return request()->domain() . __PUBLIC__ . $file;
    } else {
        return request()->server("DOCUMENT_ROOT") . __PUBLIC__ . $file;
    }
}

//设置缓存
function C_set($name, $value, $options = null, $tag = null){
    $type = config("cache.type"); //是redis就缓存
    if($type != 'File'){
        cache($name, $value, $options, $tag);
    }
    return true;
}

//得到缓存
function C_get($name, $value = '', $options = null, $tag = null){
    $type = config("cache.type"); //是redis就缓存
    if($type != 'File'){
        $data = cache($name, $value, $options, $tag);
        return $data;
    }
    return false;
}

// 建立redis对象
function redis_object(){
    $type = config("cache.type"); //是redis就缓存
    if($type != 'File' && extension_loaded("redis")) {
        static $redisObject;
        if(empty($redisObject)) {
            $redisObject = new Redis();
            $redisObject->connect(config("cache.host"), config("cache.port"), config("cache.timeout"));
            if (!empty(config("cache.password"))) {
                $redisObject->auth(config("cache.password"));
            }
            if (!empty(config("cache.select"))) {
                $redisObject->select(config("cache.select"));
            }
        }
        return $redisObject;
    }
    return false;
}